﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Xml.Linq;
using SilverlightApplication1.ServiceReference1;
using SlUtil;
using System.Text;
using System.Diagnostics;

namespace SilverlightApplication1
{
    public partial class MainControl : UserControl
    {




        public MainControl()
        {
            // Required to initialize variables
            InitializeComponent();
        }

        private void setText(object sender, ServiceReference1.GetSalariesCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                SalaryList.ItemsSource = getXmlSalariesData(decrptString(e.Result));
            }
            else
            {
                textBox1.Text = e.Error.Message;
            }
        }

        private void on_click(object sender, System.Windows.RoutedEventArgs e)
        {
            ServiceReference1.Service1SoapClient ssc = new ServiceReference1.Service1SoapClient();
            ssc.GetSalariesCompleted += setText;
            ssc.GetSalariesAsync("200903");
        }

        // TODO 需要找到一种隔离开数据层的方法
        private const string XML_NODE_SALARY = "salary";
        private const string XML_NODE_NAME = "name";
        private const string XML_NODE_OVERTIME = "overTime";

        /// <summary>
        /// 解析xml字符串，得到数据对象
        /// </summary>
        /// <param name="encrptedData">xml字符串</param>
        /// <returns>数据对象</returns>
        private List<Salary> getXmlSalariesData(string xmlStr)
        {
            // xml文件内容解析
            XDocument xdoc = XDocument.Parse(xmlStr);
            var xmlSalaries = from salary in xdoc.Descendants(XML_NODE_SALARY)
                              where salary.Element(XML_NODE_NAME) != null
                              select new Salary
                              {
                                  name = (string)salary.Element(XML_NODE_NAME),
                                  overTime = (int)salary.Element(XML_NODE_OVERTIME)
                              };
            return xmlSalaries.ToList<Salary>();
        }

        // UTF-8编码声明
        private const string UTF8_ENCODING = "UTF-8";
        // 加密密钥
        private const string AES_KEY = "1234567890xayr--";
        // 加密用IV向量
        private const string AES_IV = "1234567890xayr==";

        /// <summary>
        /// 解密数据
        /// </summary>
        /// <param name="encrptedData"></param>
        /// <returns></returns>
        private string decrptString(byte[] encrptedData)
        {
            // aes密钥长度必须为16位
            byte[] keyBytes = Encoding.GetEncoding(UTF8_ENCODING).GetBytes(AES_KEY);
            byte[] ivBytes = Encoding.GetEncoding(UTF8_ENCODING).GetBytes(AES_IV);

            // 解密字符串
            AesEncryptor aesEncpt = new AesEncryptor(keyBytes, ivBytes);
            byte[] decrptBytes = aesEncpt.DecryptString(encrptedData);
            string decrptedData = Encoding.GetEncoding(UTF8_ENCODING).GetString(decrptBytes, 0, decrptBytes.GetLength(0));
            return decrptedData;
        }
    }
}